// miniprogram/pages/issue/issue.js
const date = new Date()

const bigMonth = [1,3,5,7,8,10,12]
var getYear = date.getFullYear()
var yearIndex = getYear.toString().substring(2)
var getMonth = date.getMonth()+1
var getDate = date.getDate()
var getDay = date.getDay()
var hours = date.getHours()
var test = []
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topicName:'',
    topicA: '',
    topicB: '',
    topicC: '',
    topicD: '',
    myanswer:'A',
    radioCheck: true,
    getDay,
    timeArr:[],
    pickerSwitch:false,
    hoursTime:[],
    value:[0,0],
    indexs:0,
    hours,
    checkDate: {
      month: '',
      day: ''
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.rateTime()
    this.hoursTans()
    this.data.timeArr.forEach(m =>{
      m.day.forEach(d => {
        test.push({
          month:m.month,
          day:d.date
        })
      })
    })
    // 默认第一天
    this.setData({
      checkDate:test[0]
    })

  },
  formSubmit: function(e) {
    console.log(e);
    const {topicName, topicA,topicB, topicC, topicD} = e.detail.value;
    let mydate = new Date().getTime()
    if ( topicName == '' || topicA == ''|| topicB == ''|| topicC == ''|| topicD == '') {
      wx.showToast({
        title: '请填写完整的题目信息',
        icon: 'none'
      })
      return
    }
      wx.showModal({
        title: '确认信息',
        content: '是否提交当前的题目'
      }).then(pd =>{
        if (pd.cancel) {
          console.log('取消发布');
          return;
        }
        // 确定提交 添加到题库
        db.collection('questionBank').add({
          data: {
            A: topicA.trim(),
            B: topicB.trim(),
            C: topicC.trim(),
            D: topicD.trim(),
            title: topicName.trim(),
            answer: this.data.myanswer,
            _createTime: mydate,
            _updateTime: mydate
          }
        }).then(res =>{
          // 作业提交截至日期
          wx.removeStorageSync('deadLine')
          wx.setStorageSync('deadLine', this.data.checkDate)

          wx.showToast({
            title: '发布成功 !',
          })
          // 清空input数据
          this.setData({
            topicName:'',
            topicA: '',
            topicB: '',
            topicC: '',
            topicD: '',
            myanswer: 'A',
            radioCheck: true
          })
          console.log(res);
        })
      })
  },
   // 取消
   endpicker() {
    this.setData({
      pickerSwitch:!this.data.pickerSwitch
    })
  },
  // 确定
  confirm() {
    this.setData({
      pickerSwitch:!this.data.pickerSwitch
    })
    console.log(this.data.timeArr);
  },
  //加0
  add_zero(temp){
    if(temp<10) return "0"+temp;
    else return temp;
  },
  //判段大小月
  jugeBigMonth(month){
    let flgs = false
    bigMonth.forEach(item => {
      
      if(month == item){
        flgs = true
      }
    })
    return flgs
  },
  //星期转换
  dayTans(days){
    switch(days) {

        case 1:

              days = '星期一';

              break;

        case 2:

              days = '星期二';

              break;

        case 3:

              days = '星期三';

              break;

        case 4:

              days = '星期四';

              break;

        case 5:

              days = '星期五';

              break;

        case 6:

              days = '星期六';

              break;

        case 0:

              days = '星期日';

              break;



    }
    return days
  },
  //生成时间
  rateTime(){
    let arr = []
    for(let i = getMonth; i <= 12; i++){
       let obj = { month:i}
       let a = this.jugeBigMonth(i)
      //  console.log(a)
       if(this.jugeBigMonth(i)){

        if(getMonth == i){
          obj.day = this.rateDay(31,getDay)
        }else{
          obj.day = this.rateDay(31)
        }
       }else if( getMonth == 2){
        if(getMonth == i){
          
          if(getYear % 4 === 0 && getYear % 100 !== 0){
            obj.day = this.rateDay(29,getDay);
          }else{
            obj.day = this.rateDay(28,getDay);
          }

        }else{
          if(getYear % 4 === 0 && getYear % 100 !== 0){
            obj.day = this.rateDay(29);
          }else{
            obj.day = this.rateDay(28);
          }
        }
        
       }else{
        if(getMonth == i){
          obj.day = this.rateDay(30)
        }else{
          obj.day = this.rateDay(30)
        }
       }
       arr.push(obj)
       console.log(obj)
    }
    this.setData({
      timeArr:arr
    })
   
  },
  //日期生成
  rateDay(num,nowMonth = 1){
    let arr = []

    for(let i = nowMonth; i <= num; i++){
      let days = this.data.getDay
      let textDays = this.dayTans(days)
      arr.push({
        date:i,
        days:textDays
      })
      if(days == 6){
        days = 0
      }else{
        days+=1
      }
      this.setData({
        getDay:days
      })
    }
    return arr
  },
  //滑动
  bindChange(e){
    console.log(e)
    let index = e.detail.value[0]
    let hourIndex = e.detail.value[1]
    this.hoursTans(this.data.hoursTime[hourIndex] ,index)
    this.setData({
      checkDate:test[index]
    })

   
  },
  //小时
  hoursTans(hourIndex,index = 0){
    console.log('hourIndex',hourIndex,index)
    let hTime,
     arr = []

    if(index == 0){
      hTime = hours+1
    }else{
      hTime = 0
    }
    for(let i = hTime; i <= 23; i++){
      
      arr.push(`${this.add_zero(i)}`)
    }
    
    
    if(hourIndex) {
      let value = this.data.value
      console.log('重组之后的arr',arr)
      let nowIndex = arr.findIndex( item => {
        
        return item == hourIndex
      })
       
      console.log('value改变前',value)
      
        
        value[1] = nowIndex
        value[0] = index
        console.log('value改变后',value)
        this.setData({
          value
        })
      
    }
    
    this.setData({
      hoursTime:arr
    })
  },
  radioChange (e) {
    this.setData({
      myanswer:e.detail.value
    })
  }
})